package com.always.electrical.energy.web.controller;

import com.always.electrical.energy.web.req.*;
import com.always.electrical.energy.web.res.WebBaseResponse;
import com.always.electrical.energy.web.service.MicrogridService;
import com.always.electrical.energy.web.service.MockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import java.math.BigDecimal;

/**
 * 微网系统设置【柔直互联】
 */
@RestController
@RequestMapping("/microgrid")
public class MicrogridController {

    @Value("${web.timeOut}")
    private Long timeOut;

    @Autowired
    private MicrogridService microgridService;

    @Autowired
    private MockService mockService;

    /**
     * 阈值调控（微网系统） 正向重过载阈值 查询
     */
    @PostMapping("/threshold/positive/get")
    public DeferredResult<WebBaseResponse> thresholdPositiveGet(@RequestBody DeviceIdsReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.thresholdPositiveGet(result, req.getIds());
        return result;
    }

    /**
     * 阈值调控（微网系统） 正向重过载阈值 设置
     */
    @PostMapping("/threshold/positive/set")
    public DeferredResult<WebBaseResponse> thresholdPositiveSet(@RequestBody MicrogridThresholdPositiveReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.thresholdPositiveSet(result, req.getItems());
        return result;
    }

    /**
     * 阈值调控（微网系统） 反向重过载阈值 查询
     */
    @PostMapping("/threshold/negative/get")
    public DeferredResult<WebBaseResponse> thresholdNegativeGet(@RequestBody DeviceIdsReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.thresholdNegativeGet(result, req.getIds());
        return result;
    }

    /**
     * 阈值调控（微网系统） 反向重过载阈值 设置
     */
    @PostMapping("/threshold/negative/set")
    public DeferredResult<WebBaseResponse> thresholdNegativeSet(@RequestBody MicrogridThresholdNegativeReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.thresholdNegativeSet(result, req.getItems());
        return result;
    }

    /**
     * 阈值调控（微网系统） 储能充电阈值 查询
     */
    @PostMapping("/threshold/charge/get")
    public DeferredResult<WebBaseResponse> thresholdChargeGet(@RequestBody DeviceIdsReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.thresholdChargeGet(result, req.getIds());
        return result;
    }

    /**
     * 阈值调控（微网系统） 储能充电阈值 设置
     */
    @PostMapping("/threshold/charge/set")
    public DeferredResult<WebBaseResponse> thresholdChargeSet(@RequestBody MicrogridThresholdChargeReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.thresholdChargeSet(result, req.getItems());
        return result;
    }

    /**
     * 阈值调控（微网系统） 储能放电阈值 查询
     */
    @PostMapping("/threshold/disCharge/get")
    public DeferredResult<WebBaseResponse> thresholdDisChargeGet(@RequestBody DeviceIdsReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.thresholdDisChargeGet(result, req.getIds());
        return result;
    }

    /**
     * 阈值调控（微网系统） 储能放电阈值 设置
     */
    @PostMapping("/threshold/disCharge/set")
    public DeferredResult<WebBaseResponse> thresholdDisChargeSet(@RequestBody MicrogridThresholdDisChargeReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.thresholdDisChargeSet(result, req.getItems());
        return result;
    }

    /**
     * 并离网（微网系统）查询
     */
    @PostMapping("/gridSt/get")
    public WebBaseResponse gridStOpGet(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(microgridService.gridStOpGet(req.getIds()));
    }

    /**
     * 并离网（微网系统）设置
     */
    @PostMapping("/gridSt/set")
    public DeferredResult<WebBaseResponse> gridStOpSet(@RequestBody MicrogridGridStOpReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.gridStOpSet(result, req.getItems());
        return result;
    }

    /**
     * PCS 运行状态 召测
     */
    @PostMapping("/pcs/runningState/get")
    public WebBaseResponse pcsRunStateGet(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(microgridService.pcsRunningStateGet(req.getIds()));
    }

    /**
     * PCS 整机状态 召测
     */
    @PostMapping("/pcs/machineState/get")
    public WebBaseResponse pcsMachineStateGet(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(microgridService.pcsMachineStateGet(req.getIds()));
    }

    /**
     * PCS 运行模式 设置
     */
    @PostMapping("/pcs/runningMode/set")
    public DeferredResult<WebBaseResponse> pcsRunningModeSet(@RequestBody MicrogridPcsRunningModeReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.pcsRunningModeSet(result, req.getItems());
        return result;
    }

    /**
     * PCS 整机状态 设置
     */
    @PostMapping("/pcs/machineState/set")
    public DeferredResult<WebBaseResponse> pcsMachineStateSet(@RequestBody MicrogridPcsMachineStateReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.pcsMachineStateSet(result, req.getItems());
        return result;
    }

    /**
     * 台区负载率
     */
    @PostMapping("/pcs/rate")
    public WebBaseResponse pcsRate(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(microgridService.pcsRate(req.getIds()));
    }

    /**
     * 直流侧电压 查询
     */
    @PostMapping("/pcs/dc")
    public WebBaseResponse pcsDc(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(microgridService.pcsDc(req.getIds()));
    }

    /**
     * 交流侧电压 查询
     */
    @PostMapping("/pcs/ac")
    public WebBaseResponse pcsAc(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(microgridService.pcsAc(req.getIds()));
    }

    /**
     * 储能 运行状态 查询
     */
    @PostMapping("/storage/runningState/get")
    public DeferredResult<WebBaseResponse> storageRunningStateGet(@RequestBody DeviceIdsReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.storageRunningStateGet(result, req.getIds());
        return result;
    }

    /**
     * 储能 整机状态 查询
     */
    @PostMapping("/storage/machineState/get")
    public DeferredResult<WebBaseResponse> storageMachineStateGet(@RequestBody DeviceIdsReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.storageMachineStateGet(result, req.getIds());
        return result;
    }

    /**
     * 储能 运行模式 设置
     */
    @PostMapping("/storage/runningMode/set")
    public DeferredResult<WebBaseResponse> storageRunningModeSet(@RequestBody MicrogridStorageRunningModeReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.storageRunningModeSet(result, req.getItems());
        return result;
    }

    /**
     * 储能 整机状态 设置
     */
    @PostMapping("/storage/machineState/set")
    public DeferredResult<WebBaseResponse> storageMachineStateSet(@RequestBody MicrogridStorageMachineStateReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.storageMachineStateSet(result, req.getItems());
        return result;
    }

    /**
     * 可充放电状态 查询
     */
    @PostMapping("/storage/charge/state")
    public DeferredResult<WebBaseResponse> chargeState(@RequestBody DeviceIdsReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        mockService.chargeState(result, req.getIds());
        return result;
    }

    /**
     * 设备测功率 查询
     */
    @PostMapping("/storage/w/get")
    public DeferredResult<WebBaseResponse> wGet(@RequestBody DeviceIdsReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        microgridService.storageWGet(result, req.getIds());
        return result;
    }

    /**
     * 设备测功率 设置
     */
    @PostMapping("/storage/w/set")
    public DeferredResult<WebBaseResponse> wSet(@RequestBody MicrogridStorageWSetReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        for (MicrogridStorageWSetReq.Item item : req.getItems()) {
            BigDecimal wValue = new BigDecimal(item.getW());
            if (wValue.compareTo(new BigDecimal("-100")) < 0 || wValue.compareTo(new BigDecimal("100")) > 0) {
                result.setResult(new WebBaseResponse().setCode(400).setMsg("设备侧功率 [-100, 100]"));
                return result;
            }
        }
        microgridService.storageWSet(result, req.getItems());
        return result;
    }
}
